package br.com.puc.controllers;

import java.util.ArrayList;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.puc.models.AlunoGraduacao;
import br.com.puc.models.annotations.Publico;
import br.com.puc.repositories.AlunoGraduacaoRepository;
import br.com.puc.repositories.ProfessorRepository;

@Resource
public class AlunoGraduacaoController {

	private final Result result;
	private final AlunoGraduacaoRepository repository;
	private final ProfessorRepository professorRepository;
	
	private final Validator validator;
	
	public AlunoGraduacaoController(Result result, AlunoGraduacaoRepository repository, ProfessorRepository professorRepository, 
	Validator validator) {
		this.result = result;
		this.repository = repository;
		this.professorRepository = professorRepository;	
		this.validator = validator;
	}

	@Publico
	@Get("/alunosGraduacao")
	public ArrayList<AlunoGraduacao> index() {
		return (ArrayList<AlunoGraduacao>) repository.findAll();
	}
	
	@Post("/alunosGraduacao")
	public void create(AlunoGraduacao alunoGraduacao) {
		validator.validate(alunoGraduacao);
		validator.onErrorUsePageOf(this).newAlunoGraduacao();
		repository.create(alunoGraduacao);
		result.redirectTo(this).index();
	}
	
	@Get("/alunosGraduacao/new")
	public AlunoGraduacao newAlunoGraduacao() {
		result.include("professores", professorRepository.findAll());
		return new AlunoGraduacao();
	}
	
	@Put("/alunosGraduacao")
	public void update(AlunoGraduacao alunoGraduacao) {
		validator.validate(alunoGraduacao);
		validator.onErrorUsePageOf(this).edit(alunoGraduacao);
		repository.update(alunoGraduacao);
		result.redirectTo(this).index();
	}
	
	@Get("/alunosGraduacao/{alunoGraduacao.id}/edit")
	public AlunoGraduacao edit(AlunoGraduacao alunoGraduacao) {
		result.include("professores", professorRepository.findAll());
		return repository.find(alunoGraduacao.getId());
	}

	@Publico
	@Get("/alunosGraduacao/{alunoGraduacao.id}")
	public AlunoGraduacao show(AlunoGraduacao alunoGraduacao) {
		return repository.find(alunoGraduacao.getId());
	}

	@Delete("/alunosGraduacao/{alunoGraduacao.id}")
	public void destroy(AlunoGraduacao alunoGraduacao) {
		repository.destroy(repository.find(alunoGraduacao.getId()));
		result.redirectTo(this).index();  
	}
}